package com.frank.loadinglibrary.common;


import com.frank.loadinglibrary.common.BallLoadingComponent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;

/**
 * Created by Jack on 2015/10/19.
 */
public class BallExchangeComponent extends BallLoadingComponent {

    float[] translateX = new float[3], translateY = new float[3];

    private ArrayList<AnimatorValue> mAnimators;

    private Paint mPaint = new Paint();

    private int ballColor = Color.getIntColor("#ffffff");

    public BallExchangeComponent(Context context) {
        this(context, null);
    }

    public BallExchangeComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
        this.translateX = translateX;
    }

    public BallExchangeComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initUI();
    }

    private void initUI() {
        DrawTask task = (component, canvas) -> {
            setPaint();
            draw(canvas, mPaint);
        };
        addDrawTask(task);

        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                if (autoStart) {
                    start();
                }
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {
            }
        });
    }

    public void setPaint() {
        mPaint.setColor(new Color(ballColor));
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setAntiAlias(true);
    }

    public void start() {
        if (mAnimators == null) {
            createAnimators();
        }
        startAnimators();
    }

    public void createAnimators() {
        mAnimators = new ArrayList<>();
        float startX = getWidth() / 5;
        float startY = getWidth() / 5;
        for (int i = 0; i < 3; i++) {
            final int index = i;
            AnimatorValue translateXAnim = new AnimatorValue();
            translateXAnim.setDuration(2000);
            translateXAnim.setCurveType(Animator.CurveType.LINEAR);
            translateXAnim.setLoopedCount(-1);
            translateXAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    if (index == 1)
                        translateX[index] = translationXOne(startX, getWidth(), v);
                    else if (index == 2)
                        translateX[index] = translationXTwo(startX, getWidth(), v);
                    else
                        translateX[index] = translationXThree(startX, getWidth(), v);
                    invalidate();
                }
            });

            AnimatorValue translateYAnim = new AnimatorValue();
            translateYAnim.setDuration(2000);
            translateYAnim.setCurveType(Animator.CurveType.LINEAR);
            translateYAnim.setLoopedCount(-1);
            translateYAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
                @Override
                public void onUpdate(AnimatorValue animatorValue, float v) {
                    if (index == 1)
                        translateY[index] = translationYOne(startY, getHeight() - startY, v);
                    else if (index == 2)
                        translateY[index] = translationYTwo(startY, getHeight() - startY, v);
                    else
                        translateY[index] = translationYThree(startY, getHeight() - startY, v);
                    invalidate();
                }
            });
            mAnimators.add(translateXAnim);
            mAnimators.add(translateYAnim);
        }

    }

    @Override
    protected void stopAnimators() {
        for (AnimatorValue mAnimator : mAnimators) {
            mAnimator.stop();
        }
    }

    @Override
    protected void startAnimators() {
        for (AnimatorValue mAnimator : mAnimators) {
            mAnimator.start();
        }
    }

    @Override
    public void addDrawTask(Component.DrawTask drawTask) {
        super.addDrawTask(drawTask);
        drawTask.onDraw(this, mCanvasForTaskOverContent);
    }

    public void draw(Canvas canvas, Paint paint) {
        paint.setStrokeWidth(3);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        for (int i = 0; i < 3; i++) {
            canvas.save();
            canvas.translate(translateX[i], translateY[i]);
            canvas.drawCircle(0, 0, getWidth() / 10, paint);
            canvas.restore();
        }
    }

    protected float translationXOne(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = 3 * (b / 2 - a) * v + b / 2;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = 3 * (2 * a - b) * v + 2 * b - 3 * a;
        else if (v > (float) 2 / 3)
            res = (3 * b / 2 - 3 * a) * v + 3 * a - b;
        return res;
    }

    protected float translationXTwo(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = 3 * (2 * a - b) * v + b - a;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = (3 * b / 2 - 3 * a) * v + 2 * a - b / 2;
        else if (v > (float) 2 / 3)
            res = 3 * (b / 2 - a) * v + 2 * a - b / 2;
        return res;
    }

    protected float translationXThree(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = 3 * (b / 2 - a) * v + a;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = (3 * b / 2 - 3 * a) * v + a;
        else if (v > (float) 2 / 3)
            res = 3 * (2 * a - b) * v + 3 * b - 5 * a;
        return res;
    }

    protected float translationYOne(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = 3 * (b - a) * v + a;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = b;
        else if (v > (float) 2 / 3)
            res = 3 * (a - b) * v + 3 * b - 2 * a;
        return res;
    }

    protected float translationYTwo(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = b;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = 3 * (a - b) * v + 2 * b - a;
        else if (v > (float) 2 / 3)
            res = 3 * (b - a) * v + 3 * a - 2 * b;
        return res;
    }

    protected float translationYThree(float a, float b, float v) {
        float res = 0;
        if (v <= (float) 1 / 3)
            res = 3 * (a - b) * v + b;
        else if (v > (float) 1 / 3 && v <= (float) 2 / 3)
            res = 3 * (b - a) * v + 2 * a - b;
        else if (v > (float) 2 / 3)
            res = b;
        return res;
    }
}

